package com.smzd.ccms.complets;

import java.util.List;

import com.smzd.ccms.business.AttachmentService;
import com.smzd.ccms.core.BaseCompletPlugin;
import com.smzd.ccms.core.ParamsWrapper;
import com.smzd.ccms.domain.Attachment;
import com.smzd.ccms.domain.Channel;

public class AttachmentListComplet extends BaseCompletPlugin {
	private AttachmentService service;

	public AttachmentListComplet(int id) {
		super(id);
		service = AttachmentService.getInstance();
	}

	@Override
	public String execute(ParamsWrapper params) throws Exception {
		if (service == null) {
			synchronized (this) {
				if (service == null) {
					service = AttachmentService.getInstance();
				}
			}
		}
		Integer id = params.getParamValue("id", 0);
		List<Attachment> attachments = null;
		if (id == null || id.intValue() == 0) {
			Integer chId = params.getParamValue("chId", 0);
			if (chId == null || chId == 0) {
				Channel ch = params.getEntityWrapper().getChannel();
				chId = ch.getId();
			}
			attachments = service.getAllAttachmentsForFirstArticle(chId);
		} else {
			attachments = service.getAllAttachments(id);
		}

		if (attachments == null) {
			return null;
		}
		params.put("attachments", attachments);
		return params.getParamValue("view", "ch-art.ftl");
	}

}
